package com.example.maggie.meizinote.mvp.presenter;

import android.app.Application;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.maggie.meizinote.app.MApp;
import com.example.maggie.meizinote.app.bean.note_item;
import com.example.maggie.meizinote.mvp.ui.activity.NoteEditActivity;
import com.example.maggie.meizinote.mvp.ui.adapter.NoteListAdapter;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.example.maggie.meizinote.mvp.contract.SearchContract;

import java.util.ArrayList;
import java.util.List;


@ActivityScope
public class SearchPresenter extends BasePresenter<SearchContract.Model, SearchContract.View>
        implements BaseQuickAdapter.OnItemClickListener{
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SearchPresenter(SearchContract.Model model, SearchContract.View rootView) {
        super(model, rootView);
    }

    public void search(String key) {
        if (key == null || key.trim().isEmpty())
            mRootView.showMessage("请输入筛选的内容！");

        BmobQuery<note_item> note = new BmobQuery<note_item>();
        note.addWhereContains("content", key);

        BmobQuery<note_item> note2 = new BmobQuery<note_item>();
        note2.addWhereContains("note_title", key);

        List<BmobQuery<note_item>> queries = new ArrayList<BmobQuery<note_item>>();
        queries.add(note);
        queries.add(note2);

        BmobQuery<note_item> mainQuery = new BmobQuery<note_item>();
        mainQuery.addWhereEqualTo("user_id", MApp.getUserId());
        mainQuery.or(queries);

        mainQuery.setLimit(500);
        mainQuery.findObjects(new FindListener<note_item>() {
            @Override
            public void done(List<note_item> list, BmobException e) {
                if (e == null) {
                    mRootView.addData(list);
                } else {
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (adapter instanceof NoteListAdapter)
            //进入详情页
            mRootView.launchActivity(new Intent(mApplication, NoteEditActivity.class)
                    .putExtra("note", (note_item) adapter.getItem(position))
            );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

}
